/**
 * 
 */
package com.yidu.utils;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.Random;

import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;

import org.springframework.web.multipart.MultipartFile;

/**描述
 * @author licheng
 * @date 2019年6月25日下午2:43:59
 */
public class Tools {

	/**
	 * 生成20位的随机字符串
	 * @return
	 */
	public static String getRandom() {
		String randomStr="ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmsm");
		Random rd=new Random();
		String str="";
		for(int i=0;i<4;i++) {
			int t=rd.nextInt(randomStr.length());
			char c=randomStr.charAt(t);
			str+=c;
		}
		Date date=new Date();
		str=sdf.format(date)+str;
		return str;
	}
	/**
	 * 生成随机纯数字字符串
	 * @return
	 */
	public static String getRandomNumber() {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmsm");

		String str="";
		int c=(int)(Math.random()*1000000);
		str+=c;
		Date date=new Date();
		str=sdf.format(date)+str;
		return str;
	} 
	/**
	 * 生成随机字符串
	 * @param size
	 * @return
	 */
	public static String getRandom(int size) {
		String randomStr="ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
		Random rd=new Random();
		String str="";
		for(int i=0;i<size;i++) {
			int t=rd.nextInt(randomStr.length());
			char c=randomStr.charAt(t);
			str+=c;
		}
		return str;

	}
	/**
	 * 上传文件的工具类
	 * @param request
	 * @param name
	 * @return
	 * @throws IOException
	 * @throws ServletException
	 */
	public static String write(HttpServletRequest request,String name) throws IOException, ServletException {
		Part part=request.getPart(name);
		if(part.getSize()==0) {
			return "无照片";
		}
		//上传文件的名字
		String fileName=part.getSubmittedFileName();
		//取到upload文件夹地址
		String upload=request.getServletContext().getRealPath("upload");

		//从图片名字截取最后小数点以后的字符
		String hz=fileName.substring(fileName.lastIndexOf("."));
		//生成随机数名字+格式
		fileName=Tools.getRandom()+hz;

		part.write(upload+"/"+fileName);
		return "upload/"+fileName;
	}
	/**
	 * 	得到当前时间
	 * @return
	 */
	public static String getCurDateTime() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		return sdf.format(date);
	}
	/**
	 * 生成时间组成的编号
	 * @return
	 */
	public static String getCurDateTimeNum() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		Date date = new Date();
		return sdf.format(date);
	}
	/**
	 * 	得到当前时间
	 * @return
	 */
	public static String getCurDate() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		return sdf.format(date);
	}
	
	
	
	/**
	 * 	发送邮件的工具
	 * @param email	填写收件人的邮箱
	 * @param subject	邮件的标题
	 * @param content	邮件的内容
	 */
	public static void sendEmail(String email,String subject,String content) {
		try {
			//新建一个配置类
			Properties prop = new Properties();
			//需要发送的邮件服务器
			prop.setProperty("mail.host", "smtp.qq.com");
			//邮件发送协议smtp,不知道的请自行百度如果实在看不懂没关系，只需要按照这个写
			prop.setProperty("mail.transport.protocol", "smtp");
			//QQ邮箱的SSL安全验证
			String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
			
			prop.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
			
			prop.setProperty("mail.smtp.socketFactory.fallback", "false");
			
			//QQ邮箱端口号
			prop.setProperty("mail.smtp.port", "465");
			
			prop.setProperty("mail.smtp.socketFactory.port", "465");
			
			prop.put("mail.smtp.ssl.enable", "true");
			
			//设置smtp 授权 ,true标识允许
			prop.setProperty("mail.smtp.auth", "true");
			
			//解析来开始正确写发送邮件代码
			//首先需要创建session,此session标识邮件里面的对话 ,javax.mail.Session
			Session session = Session.getInstance(prop);
			
			//2. 通过session得到transport对象，这个对象是专门用来发送邮箱的对象
			Transport trans = session.getTransport();
			
			//3.提供用户名和密码，这里写自己的QQ号和邮箱给你的动态密码
			trans.connect("smtp.qq.com","1350921362","asxzwkafgmdjgfdh");
			
			//4.创建邮件
			Message msg = new MimeMessage(session);
			
			//发送者
			msg.setFrom(new InternetAddress("1350921362@qq.com"));
			//接受者
			msg.setRecipient(RecipientType.TO, new InternetAddress(email));
			//标题
			msg.setSubject(subject);
			//邮箱的正文 这里主要写你的服务器地址，例如http://localhost:8080/xxx/sid=激活账号
			//msg.setContent("<a href ='http://www.baidu.com'>点击这里激活账号 </a>","text/html;charset=UTF-8");
			msg.setContent(content,"text/html;charset=UTF-8");
			
			//5.发送邮箱、件
			trans.sendMessage(msg, msg.getAllRecipients());
			//6.关闭发送对象
			trans.close();
			
			System.out.println("邮件发送成功.....");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * shpingMVC 图片上传
	 * @param photo
	 * @param session
	 * @return
	 */
	public static String springImg(MultipartFile photo , HttpSession session) {
		//得到服务器的upload文件夹的地址
		String path = session.getServletContext().getRealPath("/upload");
		//System.out.println(path);//D://tomcat/springMvcDemo/upload/
		//123.jpg
		String name = photo.getOriginalFilename();//得到源文件的名字
		String[] ts = name.split("\\.");
		//取一个32位的随机字符串
		String fileName = Tools.getRandom();
		File file = new File(path+"/"+fileName+"."+ts[ts.length-1]);
		try {
			photo.transferTo(file);//将文件保存到服务器
		} catch (IllegalStateException | IOException e) { 
			e.printStackTrace();
		}
		String str = "upload/"+fileName+"."+ts[ts.length-1];
		if(name.equals("") || name == null) {
			return "" ;
		}else {
			return str;
		}
	}
}
